package smap;

import BD.DisciplinaBD;
import BD.ProfessorBD;
import BD.TurmaBD;
import java.util.HashMap;
import java.util.Set;
import javax.swing.JOptionPane;

import BD.TurmaDisciplinaBD;
import DAO.DisciplinaDAO;
import DAO.ProfessorDAO;
import DAO.TurmaDAO;
import DAO.TurmaDisciplinaDAO;
import java.util.ArrayList;
import java.util.List;

public class TelaAltTurmaDisciplina extends javax.swing.JDialog {

    TurmaDisciplinaBD tdbd = new TurmaDisciplinaBD();
    TurmaDisciplinaDAO tdbdDao = new TurmaDisciplinaDAO();
    
    //Carregar o combo de professor
    ProfessorDAO proDao = new ProfessorDAO();
    List<ProfessorBD> listaprofessor = new ArrayList<ProfessorBD>();
    
    //carregar combobox turma
    TurmaDAO turDao = new TurmaDAO();
    List<TurmaBD> listaturma = new ArrayList<TurmaBD>();
    
    //carregar combobox disciplina
    DisciplinaDAO disDao = new DisciplinaDAO();
    List<DisciplinaBD> listadisciplina = new ArrayList<DisciplinaBD>();
    
    public TelaAltTurmaDisciplina(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDisciplina = new javax.swing.JPanel();
        lblDisciplina = new javax.swing.JLabel();
        cmbSemestre = new javax.swing.JComboBox();
        lblSemestre = new javax.swing.JLabel();
        lblProfessor = new javax.swing.JLabel();
        btnAlterar = new javax.swing.JButton();
        cmbDisciplina = new javax.swing.JComboBox();
        cmbProfessor = new javax.swing.JComboBox();
        pnlPesquisarTurma = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cmbTurma = new javax.swing.JComboBox();
        pnlVoltar = new javax.swing.JPanel();
        btnVoltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alterar disciplina-turma");
        setResizable(false);

        pnlDisciplina.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Alterar disciplina na turma"));

        lblDisciplina.setText("Disciplina:");

        cmbSemestre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));

        lblSemestre.setText("Semestre:");

        lblProfessor.setText("Professor:");

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnAlterar.setText("Salvar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        cmbDisciplina.setEnabled(false);

        javax.swing.GroupLayout pnlDisciplinaLayout = new javax.swing.GroupLayout(pnlDisciplina);
        pnlDisciplina.setLayout(pnlDisciplinaLayout);
        pnlDisciplinaLayout.setHorizontalGroup(
            pnlDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDisciplinaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDisciplinaLayout.createSequentialGroup()
                        .addComponent(lblSemestre)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblProfessor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                    .addGroup(pnlDisciplinaLayout.createSequentialGroup()
                        .addComponent(lblDisciplina)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbDisciplina, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnlDisciplinaLayout.setVerticalGroup(
            pnlDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDisciplinaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDisciplina)
                    .addComponent(cmbDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(pnlDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSemestre)
                    .addComponent(lblProfessor)
                    .addComponent(btnAlterar)
                    .addComponent(cmbProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pnlPesquisarTurma.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray), "Turma"));

        jLabel2.setText("Nome da turma:");

        cmbTurma.setEnabled(false);

        javax.swing.GroupLayout pnlPesquisarTurmaLayout = new javax.swing.GroupLayout(pnlPesquisarTurma);
        pnlPesquisarTurma.setLayout(pnlPesquisarTurmaLayout);
        pnlPesquisarTurmaLayout.setHorizontalGroup(
            pnlPesquisarTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisarTurmaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbTurma, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlPesquisarTurmaLayout.setVerticalGroup(
            pnlPesquisarTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPesquisarTurmaLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnlPesquisarTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        pnlVoltar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlVoltarLayout = new javax.swing.GroupLayout(pnlVoltar);
        pnlVoltar.setLayout(pnlVoltarLayout);
        pnlVoltarLayout.setHorizontalGroup(
            pnlVoltarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlVoltarLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(188, 188, 188))
        );
        pnlVoltarLayout.setVerticalGroup(
            pnlVoltarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlVoltarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlDisciplina, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlPesquisarTurma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlVoltar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnlPesquisarTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents
    //Carregar Turma
    public void carregarComboTurma(int ano){ //de acordo com o ano        
        cmbTurma.removeAllItems();//limpar combo
        listaturma.removeAll(listaturma); //limpar a lista
        listaturma = turDao.carregarComboAno(ano);
        
        for(int i = 0; i<listaturma.size(); i++){
            cmbTurma.addItem(listaturma.get(i).getNome());
        } 
    }
//    
//    //Carregar Disciplinas
        public void carregarComboDisciplina(){
            cmbDisciplina.removeAllItems();//limpar combo
            listadisciplina.removeAll(listadisciplina); //limpar a lista
            listadisciplina = disDao.carregarCombo();

            for(int i = 0; i<listadisciplina.size(); i++){
                cmbDisciplina.addItem(listadisciplina.get(i).getNome());
            } 
        }
//    
    public void carregarComboProfessor(){
        listaprofessor = proDao.carregarComboProfessor();
       
        for(int i = 0; i<listaprofessor.size(); i++){
            cmbProfessor.addItem(listaprofessor.get(i));
        }
    }
 
  // Método para alterar os dados do Curso
    public void recebeObjejo (TurmaDisciplinaBD td){
        //cmbArea.setSelectedItem(ABORT);
        cmbTurma.setSelectedItem(td.getNomeTurma());
        cmbProfessor.setSelectedIndex(td.getCodProfessor());
        cmbDisciplina.setSelectedIndex(td.getCodDisciplina());        
        cmbSemestre.setSelectedIndex(td.getSemestre() -1 );         
    }
    
    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        TurmaDisciplinaBD newTurmaDis = new TurmaDisciplinaBD();
        
        if(cmbProfessor.getSelectedIndex() == -1 || cmbProfessor.getSelectedIndex() == 0)
        {
            JOptionPane.showMessageDialog(null,"Selecione um professor para a disciplina.", 
                    "Selecione o professor", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            newTurmaDis.setCodTurma(listaturma.get(cmbTurma.getSelectedIndex()).getCodigo());
            newTurmaDis.setCodDisciplina(listadisciplina.get(cmbDisciplina.getSelectedIndex()).getCodigo());
            newTurmaDis.setCodProfessor(listaprofessor.get(cmbProfessor.getSelectedIndex()).getCodigo());
            newTurmaDis.setSemestre(Integer.parseInt(cmbSemestre.getSelectedItem().toString()));
            
            if(tdbdDao.alterarTurmaDisciplina(newTurmaDis)){
                this.dispose();
            }
        }      
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        this.dispose(); // Fecha Tela Curso
    }//GEN-LAST:event_btnVoltarActionPerformed

    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaAltTurmaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaAltTurmaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaAltTurmaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaAltTurmaDisciplina.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaAltTurmaDisciplina dialog = new TelaAltTurmaDisciplina(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JComboBox cmbDisciplina;
    private javax.swing.JComboBox cmbProfessor;
    private javax.swing.JComboBox cmbSemestre;
    private javax.swing.JComboBox cmbTurma;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblDisciplina;
    private javax.swing.JLabel lblProfessor;
    private javax.swing.JLabel lblSemestre;
    private javax.swing.JPanel pnlDisciplina;
    private javax.swing.JPanel pnlPesquisarTurma;
    private javax.swing.JPanel pnlVoltar;
    // End of variables declaration//GEN-END:variables
}
